package utils;

import com.leha.core.constants.CoreErrorDict;
import constants.APIErrorDict;

/**
 * Created by xiabin on 2017/8/14.
 */
public class APIResult {

    private int resultCode;

    private String resultMsg;

    private Object data;

    private APIResult() {

    }

    public APIResult(CoreErrorDict err) {
        data = null;
        resultCode = err.getErrCode();
        resultMsg = err.getErrMsg();
    }

    public APIResult(APIErrorDict err) {
        data = null;
        resultCode = err.getCode();
        resultMsg = err.getMsg();
    }

    public APIResult(Object data, ReturnStrategy returnStrategy) {
        this(data);
        if (returnStrategy == ReturnStrategy.NONE_NULL) {
            if (data == null) {
                this.resultCode = CoreErrorDict.RESULT_ERROR.getErrCode();
                this.resultMsg = CoreErrorDict.RESULT_ERROR.getErrMsg();
            }
        }
    }

    public APIResult(Object resData) {
        resultCode = 0;
        resultMsg = "OK";
        data = resData;
    }

    public APIResult(String msg) {
        this.resultCode = 0;
        this.resultMsg = msg;
    }

    public static APIResult error(String message) {
        APIResult result = new APIResult(CoreErrorDict.SYSTEM_ERROR);
        result.setResultMsg(message);
        return result;
    }

    public int getResultCode() {
        return resultCode;
    }

    public void setResultCode(int resultCode) {
        this.resultCode = resultCode;
    }

    public String getResultMsg() {
        return resultMsg;
    }

    public void setResultMsg(String resultMsg) {
        this.resultMsg = resultMsg;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    public enum ReturnStrategy {
        WITH_NULL,
        NONE_NULL
    }
}